// mobile bind
// ============

define(['zepto', 'utils', 'countDown'], function($, utils){
  return {
    init: function(data) {
      
      this.token = null;
      utils.configWeixin(data.jsConfig, false);
      this.bindMobileBindingPageEvent(data);
    },

    /**
     * Bind mobile binding page event
     */
    bindMobileBindingPageEvent: function(data) {
      var that = this;
      
      var checkedGroup = $("input[name='groupid']:checked")
      
      // On submiting mobile
      $(document).on('click', '#submit', function(e) {
        e.preventDefault();
        var checkedGroup = $("input[name='groupid']:checked");
        if(checkedGroup.length == 0) {
        	$.toast("请选择群")
        	return false;
        }
        
        var submiting = that.submitMobile(checkedGroup.val(), data.opengid)
        $.when(submiting).done(function(data) {
        	console.log(data);
        	$("#submit").addClass("disabled");
        	$("#submit").prop("disabled", true);
        })
      })
    },

    /**
     * Submit mobile and code for binding
     */
    submitMobile: function(gid, opengid) {
      var that = this, defer = $.Deferred()
      $.showIndicator()
      $.post(window.__CTX__.root + '/wechat-groups/bind?opengid=' + opengid + '&gid=' + gid)
        .done(function(response) {
          $.hideIndicator()
          utils.processResult(response, function(result) {
            $.toast(result.message || '绑定成功')
            defer.resolve(result.data)
          }, function(result) {
            $("#submit").data("submiting", "false");
            $.toast(result.message || '绑定失败')
          })
        })
        
      return defer.promise()
    }
  }

})